package com.gorkr.labuladong.data_struct.array;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/07/26 15:06
 **/
public class M153FindMinimumInRotatedSortedArray {

    public int findMin(int[] nums) {

        int n = nums.length;
        if(n==1){
            return nums[0];
        }

        int left = 0, right = n - 1;

        while (left <= right) {
            int mid = (left + right) / 2;

            // 处理越界问题
            if (nums[mid] > nums[(mid + 1) % n]) {
                return nums[(mid + 1) % n];
            } else if (nums[left] <= nums[mid]) {
                // 左边有序在右边找
                left = mid + 1;
            } else {
                // 右边有序 在左边找
                right = mid - 1;
            }
        }
        return -1;
    }

    @Test
    public void test() {

        int[] nums = {3, 4, 5, 1, 2};
        int min = findMin(nums);
        System.out.println(min);

    }
}
    